package com.rrx.controller;

import com.rrx.model.Address;
import com.rrx.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
public class AddressController {
    @Autowired
    AddressService addressService;
    /**
     * 添加一条地址
     *
     * @param
     * @return
     */
    @RequestMapping("/Save")
    @ResponseBody



    public String saveAddress(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        System.out.println("coming save");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Long dates = System.currentTimeMillis();//获取系统时间戳
        System.out.println(dates);

        String province = req.getParameter("sheng");
        String city = req.getParameter("shi");
        String area = req.getParameter("qu");
        String xxdz = req.getParameter("xxdz"); // 详细地址
        String userid = req.getParameter("yhid");

        System.out.println(province + "  " + city + "  " + area + "  " + xxdz);
        Integer count = addressService.addAddress(province, city, area, xxdz, userid, dates);
//        JSONObject jsonObject = new JSONObject();
//



//        if (count > 0) {
//            jsonObject.put("success","添加成功");
//            jsonObject.put("error","11");
//
//        } else {
//            jsonObject.put("success","添加失败");
//            jsonObject.put("error","22");
//
//        }
        if(count > 0)
            return "success";
        else
            return "error";
    }

    /**
     * 删除一条地址
     *
     * @param
     * @return
     */
    // @Delete(" DELETE FROM address WHERE id=#{id}")
    @RequestMapping("/Remove")
    public Integer removeAddress(HttpServletRequest req) {
        ModelAndView modelAndView = new ModelAndView();
        return null;
    }


    /**
     * 查询一条地址
     *
     * @param
     * @return
     */
    @RequestMapping("/Find")
    public ModelAndView findAddress(HttpServletRequest req) {
        ModelAndView modelAndView = new ModelAndView();
        Integer userid = Integer.parseInt( req.getParameter("userid"));
        ArrayList<Address> lists= addressService.seleAddress(userid);
        modelAndView.setViewName("index");
        if (lists.size() > 0) {

            modelAndView.addObject(lists);
        } else {
            modelAndView.addObject(null);
        }
        return modelAndView;
    }

    /**
     * 修改一条地址
     * @param
     * @return
     */
    @RequestMapping("/Modify")
    public Integer modifyAddress (){
        return null;
    }
}
